package com.yanqu.road.dao.impl.palace;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.estatewar.palace.PalaceBattleNotice;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class PalaceBattleNoticeDaoImpl extends TempDataObjectDao<PalaceBattleNotice> {

    public List<PalaceBattleNotice> getPalaceBattleNoticeList(int activityId, int maxCount) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<PalaceBattleNotice> dataList = new ArrayList<>();
        String sql = "select * from t_c_palace_notice where activityId=? ORDER BY noticeTime DESC LIMIT ?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            call.setInt(2, maxCount);
            rs = call.executeQuery();
            while (rs.next()) {
                PalaceBattleNotice data = parse(rs);
                dataList.add(data);
            }
            dataList.sort(Comparator.comparingLong(PalaceBattleNotice::getNoticeTime));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    @Override
    protected boolean addData(PalaceBattleNotice data) {
        PreparedStatement call = null;
        String sql = "insert into t_c_palace_notice(noticeUid,activityId,isWin,noticeTime,userId,userInfo,enemyUserId,enemyUserInfo,pos) " +
                "values (?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getNoticeUid());
            call.setInt(i++, data.getActivityId());
            call.setBoolean(i++, data.isWin());
            call.setLong(i++, data.getNoticeTime());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getUserInfoJsonStr());
            call.setLong(i++, data.getEnemyUserId());
            call.setString(i++, data.getEnemyUserInfoJsonStr());
            call.setInt(i++, data.getPos());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(PalaceBattleNotice data) {
        return false;
    }

    @Override
    protected PalaceBattleNotice parseData(ResultSet rs) throws SQLException {
        PalaceBattleNotice data = new PalaceBattleNotice();
        data.setNoticeUid(rs.getString("noticeUid"));
        data.setActivityId(rs.getInt("activityId"));
        data.setWin(rs.getBoolean("isWin"));
        data.setNoticeTime(rs.getLong("noticeTime"));
        data.setUserId(rs.getLong("userId"));
        data.setEnemyUserId(rs.getLong("enemyUserId"));
        String param = rs.getString("userInfo");
        if(null == param){
            param = "{}";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(param, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setUserInfo(userBaseInfo);
        }
        param = rs.getString("enemyUserInfo");
        if(null == param){
            param = "{}";
        }
        userBaseInfo = JSON.parseObject(param, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setEnemyUserInfo(userBaseInfo);
        }
        data.setPos(rs.getInt("pos"));
        return data;
    }
}
